'''
Created on Oct 19, 2011

@author: box
'''''

import matplotlib.pyplot as plt
import matplotlib.colors as clv
import matplotlib.mlab as mlb
from scipy.interpolate import griddata
import math as m
from mpl_toolkits.basemap import Basemap
import numpy as np
from pylab import load, meshgrid, title, arange, show
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

def smoothArray(a,digs):
    tempArray=[]
    cto=0
    for i in a:
        tempArray.append(round(a[cto],digs))
        cto=cto+1
    tempArray = np.array(tempArray)
    return tempArray

def mean(numberList):
    if len(numberList) == 0:
        return float('nan')
 
    floatNums = [float(x) for x in numberList]
    return sum(floatNums) / len(numberList)

def convertToDaysAfter2004(yearmonthdayStr):
    attr = yearmonthdayStr.split(",")
    yeardiff=0
    monthdiff=0
    dayDiff=0
    
    yeardiff = int(attr[0])-2004
    monthdiff = int(attr[1])
    dayDiff = int(attr[2])
    
    result = (yeardiff*365)+(monthdiff*31)+dayDiff
    return result


# Read in social-spatial data
infile = open('outputReduced2004.csv','r')
lats = []
lons = []
z = []
zs =[]
cto=0
resolution=1000
currMn=100000000
currMx=0
dataVal=11;
minLat=600
maxLat=-200
minLon=600
maxLon=-200
currMx=0
currData=27-3
#Word file index + offset
for line in infile:
   cto=cto+1
   
   cols = line.split(",")
   if(cto>2 and cols[3]!=""):
       if(float(cols[3])>27and float(cols[3])<38 and float(cols[4])>38 and float(cols[4])<48):
           if(float(cols[3])>maxLat):
               maxLat=float(cols[3])
           if(float(cols[3])<minLat):
               minLat=float(cols[3])
           if(float(cols[4])>maxLon):
               maxLon=float(cols[4])
           if(float(cols[4])<minLon):
               minLon=float(cols[4])
               
           lats.append(float(cols[3]))
           lons.append(float(cols[4]))
           z.append(float(cols[dataVal]))
      
#2:pop,3:

currMn = min(z)
currMean= mean(z)
currMx = max(z)

#print lats


# Plotting routines expect numpy arrays
fig=plt.Figure()
lat = np.array(lats)
lon = np.array(lons)
ZST= np.array(z)
Z = np.array(z)


    


range= currMx-currMn
ZAL = []
ZSTr=[]
print minLat
print maxLat
print minLon
print maxLon
'''
ct=0
for entry in Z:
    alpha= ((Z[ct]-currMn)/range)
    ZAL.append((0,((Z[ct]-currMn)/range),0,alpha))
    ct=ct+1
'''
ct=0
for entry in ZST:
    alpha= ((Z[ct]-currMn)/range)
    ZSTr.append(alpha)
    ct=ct+1    



#print ZSTr
#Z=np.array(ZAL)
ZST = np.array(ZSTr)

map = Basemap(resolution='i',projection='mill',llcrnrlon=minLon,llcrnrlat=minLat,urcrnrlon=maxLon,urcrnrlat=maxLat)
map.fillcontinents(color='brown',lake_color='aqua',zorder=0)
map.drawcoastlines()
map.drawcountries()
map.drawstates()
map.drawmapboundary() # draw a line around the map region
map.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
map.drawmeridians(np.arange(-180.,181.,20.),labels=[0,0,0,1])
map.bluemarble()
map.drawmapboundary(fill_color='blue') 
#map.bluemarble()

x, y = map(lon, lat)

myplot= map.scatter(x,y,s=Z,c=Z,marker='o',cmap=plt.cm.gist_rainbow,linewidths=0,faceted=False)
#myplot.size=0
plt.colorbar()

canvas=FigureCanvas(fig)

plt.xticks([]) #Nothing on the axes for now
plt.yticks([])
plt.show()

 # danger, data can be corrupted if this isn't invoked



